<template>
  <div>
    <el-button type="primary" @click="start()" style = "margin:8px;" size = "mini">生成优惠码</el-button>
    <el-table :data="tableData" border v-loading="loading" :header-cell-style="{'text-align':'center'}"
              :cell-style="{'text-align':'center'}">
      <el-table-column fixed prop="id" label="编号" width="150"></el-table-column>
      <el-table-column prop="uuid" label="优惠码" width="500"></el-table-column>
      <el-table-column prop="state" label="状态" width="150"></el-table-column>
      <el-table-column label="操作" width="320">
        <template slot-scope="scope">
          <el-button slot="reference" type="success" icon="el-icon-edit" @click="editCoupon(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除吗?" @confirm="delCoupon(scope.row)">
            <el-button slot="reference" type="warning" icon="el-icon-delete">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="prev, pager, next"
                   :total="total" page-size="5" @current-change="page"
                   style="margin:20px;">
    </el-pagination>
  </div>
</template>

<script>
  const axios = require('axios')
  const isNum = (rule, value, callback) => {
    const age = /^[0-9]*$/
    if (!age.test(value)) {
      callback(new Error('年龄只能为数字'))
    } else {
      callback()
    }
  }
  export default {
    name: "Coupon",
    data() {
      return {
        ruleForm: {
          state: ''
        },
        rules: {

        },
        id: '',
        uuid: '',
        state: '',
        loading: false,
        pageSize: '1',
        total: null,
        tableData: null
      };
    },
    methods: {
      start() {
        const _this = this
        axios.post('http://localhost:9000/coupon/create').then(function (resp) {
          console.log(resp)
          if (resp.data == 'error') {
            return false;
          } else {
            const result = resp.data
            _this.$alert('优惠码  ' + result, '消息', {
              confirmButtonText: '确认',
              callback: action => {
                window.location.reload()
                this.$message({
                  type:'success',
                  message:'优惠码已生成'
                })
              }
            });
          }
        })
      },
      editCoupon(row){
        this.$router.push({
          path: '/myUpdate/updateCoupon',
          query: {
            id: row.id
          }
        });
      },
      delCoupon(row) {
        const _this = this
        axios.delete('http://localhost:9000/coupon/delById/' + row.id).then(function (resp) {
          _this.$alert('优惠码<' + row.id + '>已删除', '消息', {
            confirmButtonText: '确定',
            callback: action => {
              window.location.reload()
            }
          });
        })
      },
      page(currentPage) {
        const _this = this
        this.$axios.get('http://localhost:9000/coupon/findPage/' + currentPage + '/5').then(function (resp) {
          _this.tableData = resp.data.content
          _this.pageSize = resp.data.size
          _this.total = resp.data.totalElements
        })
      }
    },
    created() {
      const _this = this
      this.$axios.get('http://localhost:9000/coupon/findPage/1/5').then(function (resp) {
        _this.tableData = resp.data.content
        _this.pageSize = resp.data.size
        _this.total = resp.data.totalElements
      })
    },
  }
</script>

<style scoped>

</style>
